package com.sgb.member.view_model;

import com.sgb.member.PickCateType;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberPersonBean;
import com.sgb.member.ui.fragment.PersonTypeDemandFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class PersonTypeDemandModel extends BaseViewModel {

    private final PersonTypeDemandFragment fragment;
    private MemberRepertory repertory;

    private int current;
    private CommonPopEntity mAll;

    public PersonTypeDemandModel(PersonTypeDemandFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;

        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }

    // 解除会员申请
    public void relieveMember(int pos, String no) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().personMemberRelieve(no).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                MToast.showToast(fragment.getContext(), "解除会员成功");
                fragment.removeMember(pos);
            }
        });
    }

    // 会员调级
    public void sendGradeApprove(int pos, String no, String userLevel) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        // 会员调级
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据加载中...");
        Map<String, Object> map = new HashMap<>();
        map.put("no", no);
        map.put("level", userLevel);
        map.put("type", "1");
        repertory.getApi().sendGradeApprove(map).enqueue(new ICallBack<BaseEntity<BaseEntity<Object>>>() {
            @Override
            public void onSuccess(BaseEntity<BaseEntity<Object>> data) {
                MToast.showToast(fragment.getContext(), "会员调级成功");
                fragment.updateMemberLevel(pos, userLevel);
            }
        });
    }

    public void userFocus(String businessNo, boolean isGz, int position) {
        Map<String, Object> map = new HashMap<>();
        map.put("businessNo", businessNo);
        map.put("type", 0); // 固定值0
        DialogHelper.showProgressDialog(fragment.getContext(), null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().userFocus(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                fragment.updateMemberFollow(isGz, position);
            }
        });
    }

    public void getMyMembers(boolean isLoadMore, String companyName, String mNo0, String mNo1, String mNo2, String mNo3, String mNo4, String mNo41, String mNo5, String mNo6, String mNo7, String mNo8, String mNo9, String mNo10) {
        int currentPage = isLoadMore ? current + 1 : 1;
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("companyName", companyName); //搜索企业名称
        map.put("memberType", StringUtil.isEmpty(mNo1) ? mNo0 : mNo1); //会员类型
//        map.put("secondMemberType", mNo1); //会员类型二级
        map.put("companyType", mNo2); //企业类型
        map.put("level", mNo3); //企业等级
        map.put("provinceNo", mNo4); //经营地址 省【add】
        map.put("cityNo", mNo41); //经营地址 市【add】
        map.put("qualificationType", mNo5); //资质类型【add】
        map.put("honourType", mNo6); //荣誉分类【add】
        map.put("assetCategories", mNo7);//固定资产分类【add】
        map.put("knowledgeType", mNo8); //知识产权分类【add】
        map.put("moneyType", mNo9); //单次金额最大【add】
        map.put("isHaveZc", mNo10); //是否有招采信息【add】
        map.put("relation", "1"); //会员关系 1供方 2需方
        map.put("limit", 20);
        map.put("page", currentPage);
        repertory.getApi().getMemberLeagueCompanyList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberPersonBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberPersonBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberPersonBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if(!isLoadMore){
                    fragment.showEmpty();
                }
            }
        });
    }

    public void getJoinMembers(boolean isLoadMore, String companyName, String mNo0, String mNo1, String mNo2, String mNo3, String mNo4, String mNo41, String mNo5, String mNo6, String mNo7, String mNo8, String mNo9, String mNo10) {
        int currentPage = isLoadMore ? current + 1 : 1;
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("companyName", companyName); //搜索企业名称
        map.put("memberType", StringUtil.isEmpty(mNo1) ? mNo0 : mNo1); //会员类型
//        map.put("secondMemberType", mNo1); //会员类型二级
        map.put("companyType", mNo2); //企业类型
        map.put("level", mNo3); //企业等级
        map.put("provinceNo", mNo4); //经营地址 省【add】
        map.put("cityNo", mNo41); //经营地址 市【add】
        map.put("qualificationType", mNo5); //资质类型【add】
        map.put("honourType", mNo6); //荣誉分类【add】
        map.put("assetCategories", mNo7);//固定资产分类【add】
        map.put("knowledgeType", mNo8); //知识产权分类【add】
        map.put("moneyType", mNo9); //单次金额最大【add】
        map.put("isHaveZc", mNo10); //是否有招采信息【add】
        map.put("relation", "2"); //会员关系 1供方 2需方
        map.put("limit", 20);
        map.put("page", currentPage);
        repertory.getApi().getMemberLeagueCompanyList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberPersonBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberPersonBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberPersonBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if(!isLoadMore){
                    fragment.showEmpty();
                }
            }
        });
    }

    public void getLeftChooseData(int type, boolean isShowLeftDialog, boolean isDemandMember, String no) {
        Call<BaseEntity<List<CommonPopEntity>>> call = null;
        switch (type) {
            case PickCateType.TYPE_0:
                // 会员主体
                List<CommonPopEntity> listMain = new ArrayList<>();
                listMain.add(new CommonPopEntity("1", "企业会员"));
                listMain.add(new CommonPopEntity("0", "个人会员"));
                if (isShowLeftDialog) {
                    fragment.showLeftChooseDialog(type, listMain);
                } else {
                    fragment.setLeftChooseData(type, listMain);
                }
                break;
            case PickCateType.TYPE_01:
                // 会员类型 0 供方 1需方
                String ascription = isDemandMember ? "1" : "0";
                call = repertory.getApi().getMemberTypeList(ascription);
                break;
            case PickCateType.TYPE_1:
                // 会员类型二级
                call = repertory.getApi().getMemberTypeSubList(no);
                break;
            case PickCateType.TYPE_2:
                // 企业类型
                call = repertory.getApi().getCompanyTypeList();
                break;
            case PickCateType.TYPE_3:
                // 会员等级
                call = repertory.getApi().getMemberLevelList();
                break;
            case PickCateType.TYPE_4:
                // 资质类型
                call = repertory.getApi().getQualifyTypeTreeList();
                break;
            case PickCateType.TYPE_5:
                break;
            case PickCateType.TYPE_6:
                // 荣誉分类
                call = repertory.getApi().getHonorTypeList();
                break;
            case PickCateType.TYPE_7:
                // 固定资产
                call = repertory.getApi().getAssetType();
                break;
            case PickCateType.TYPE_8:
                // 知识产权
                call = repertory.getApi().getmemberIntellectualPropertyRightType();
                break;
            case PickCateType.TYPE_9:
                // 单次业绩最大金额
                call = repertory.getApi().performanceAmountList();
                break;
            case PickCateType.TYPE_10:
                // 是否有招采信息，1有0没有
                List<CommonPopEntity> list = new ArrayList<>();
                list.add(mAll);
                list.add(new CommonPopEntity("1", "是"));
                list.add(new CommonPopEntity("0", "否"));
                if (isShowLeftDialog) {
                    fragment.showLeftChooseDialog(type, list);
                } else {
                    fragment.setLeftChooseData(type, list);
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + type);
        }
        if (call == null) {
            DialogHelper.dismissProgressDialog();
            return;
        }
        call.enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null) {
                    if (type == PickCateType.TYPE_01
                            || type == PickCateType.TYPE_1
                            || type == PickCateType.TYPE_2
                            || type == PickCateType.TYPE_4
                            || type == PickCateType.TYPE_6
                            || type == PickCateType.TYPE_7
                    ) {
                        data.getData().add(0, mAll);
                    }
                    if (isShowLeftDialog) {
                        fragment.showLeftChooseDialog(type, data.getData());
                    } else {
                        fragment.setLeftChooseData(type, data.getData());
                    }
                }else{
                    if (isShowLeftDialog) {
                        fragment.showLeftChooseDialog(type, new ArrayList<>());
                    } else {
                        fragment.setLeftChooseData(type, new ArrayList<>());
                    }
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据加载中...");
        repertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setAddressData(data.getData());
                }
            }
        });
    }

    private List<CommonPopEntity> mLeverData;

    public void getMemberLevelList(int pos, MemberBean bean) {
        if (mLeverData == null || mLeverData.size() == 0) {
            DialogHelper.showProgressDialog(fragment.getActivity(), "数据加载中...");
            repertory.getApi().getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
                @Override
                public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                    if (data.getData() != null && data.getData() != null) {
                        if (data.getData().get(0).getTypeName().equals("全部")) {
                            data.getData().remove(0);
                        }
                        mLeverData = new ArrayList<>();
                        mLeverData.addAll(data.getData());
                        fragment.showLeverPop(pos, bean, mLeverData);
                    }
                }
            });
        } else {
            fragment.showLeverPop(pos, bean, mLeverData);
        }
    }
}
